class TreeNode:
    def __init__(self, val):
        self.val = val
        self.neighbors = {}
        self.count = 0
        self.flag = 0


class Trie(object):

    def __init__(self):
        self.tree = TreeNode('#')

    def insert(self, word):
        """
        :type word: str
        :rtype: None
        """
        node = self.tree
        for w in word:
            if w in node.neighbors:
                node = node.neighbors[w]
            else:
                new_node = TreeNode(w)
                node.neighbors[w] = new_node
                node = new_node
            node.count += 1
        node.flag += 1

    def countWordsEqualTo(self, word):
        """
        :type word: str
        :rtype: int
        """
        node = self.tree
        flag = True
        for w in word:
            if w in node.neighbors:
                node = node.neighbors[w]
            else:
                flag = False
                break
        if flag:
            return node.flag
        else:
            return 0


    def countWordsStartingWith(self, prefix):
        """
        :type prefix: str
        :rtype: int
        """
        node = self.tree
        flag = True
        for w in prefix:
            if w in node.neighbors:
                node = node.neighbors[w]
            else:
                flag = False
                break
        if flag:
            return node.count
        else:
            return 0

    def erase(self, word):
        """
        :type word: str
        :rtype: None
        """
        node = self.tree
        for w in word:
            node = node.neighbors[w]
            node.count -= 1
        node.flag -= 1

